package cz.proks.easyhelpdesk.gae.core.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.proks.easyhelpdesk.api.TicketApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.Ticket;

@Component
public class TicketManager extends AManager<Ticket> implements TicketApi {
	private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	@Qualifier(Configuration.Core.DB.TICKET_API)
	private TicketApi dbApi;
	
	@Override
	protected TicketApi getDbApi() {
		return dbApi;
	}	
	
	@Override
	public Long create(Ticket entity) throws ApiException {
	    log.debug("create():: Start with ticket='{}'", entity);
	    
	    if (entity != null && (entity.getThreadId() == null || entity.getThreadId() <= 0))	        	       
	        entity.setThreadId((long)createUniqueIntegerID());	    
	    
	    Long id = super.create(entity);
	    
	    log.debug("create():: End with id '{}'", id);
	    return id;
	}
}
